package com.zxpt.quetion.service.impl;

import com.zxpt.quetion.mappervo.QuItOpMapperVo;
import com.zxpt.quetion.pojovo.QuItOpVo;
import com.zxpt.quetion.pojovo.QuestionOptionsVo;
import com.zxpt.quetion.service.IQuestionOptionsService;
import com.zxpt.quetion.mappervo.QuestionOptionsMapperVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;

import java.util.Collections;
import java.util.Map;
import java.util.List;
import javax.annotation.Resource;
import  com.zxpt.quetion.utils.Query;
import  com.zxpt.quetion.utils.CreateQueryConditionUtil;
import  com.zxpt.quetion.utils.SqlUtil;
import org.springframework.util.StringUtils;

/**
* 所在包：com.zxpt.quetion.service.impl.QuestionOptionsServiceImpl
* 文件名：QuestionOptionsServiceImpl
* 版权：稻草人.
* 描述：
* 修改人：rmk
* 修改时间：
*/
@Service
@Transactional
@Slf4j
public class QuestionOptionsServiceImpl implements IQuestionOptionsService {

    @Resource
    private QuestionOptionsMapperVo questionOptionsMapperVo;
    @Resource
    private QuItOpMapperVo quItOpMapperVo;

    @Override
    public PageInfo<QuestionOptionsVo> getPage (Map<String, Object> queryMap, Integer page, Integer rows) {
        Query query = CreateQueryConditionUtil.createCondition(queryMap);
        PageHelper.startPage(page, rows);
        List<QuestionOptionsVo> list = questionOptionsMapperVo.getPage(query.getSql(), queryMap);
        PageInfo<QuestionOptionsVo> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public QuestionOptionsVo getById(String id) {
        QuestionOptionsVo questionOptionsVo = questionOptionsMapperVo.getById(id);
        return questionOptionsVo;
    }

    @Override
    public void add(QuestionOptionsVo questionOptionsVo) {

    }

    //改造add，又中间表和children需要插
    public void add(QuestionOptionsVo questionOptionsVo,String queID,Integer sort) {
        if(StringUtils.isEmpty(questionOptionsVo.getPid())){
            questionOptionsVo.setPid("#");
        }
        if(StringUtils.isEmpty(questionOptionsVo.getValue())){
            questionOptionsVo.setValue(questionOptionsVo.getLabel());
        }
        SqlUtil<QuestionOptionsVo> sqlUtil = new SqlUtil<>();
        String sql = sqlUtil.createInsertSql(QuestionOptionsVo.class, questionOptionsVo);
        questionOptionsMapperVo.insertSelective(questionOptionsVo, sql);
        String opId = questionOptionsVo.getId();
        QuItOpVo quItOpVo = new QuItOpVo();
        quItOpVo.setItemId(queID);
        quItOpVo.setOpId(opId);
        quItOpVo.setSort(sort);
        SqlUtil<QuItOpVo> sqlUtil_ItOp = new SqlUtil<>();
        sql = sqlUtil_ItOp.createInsertSql(QuItOpVo.class, quItOpVo);
        quItOpMapperVo.insertSelective(quItOpVo, sql);
        if(CollectionUtils.isNotEmpty(questionOptionsVo.getChildren())){
            List<QuestionOptionsVo> opChildren_list = questionOptionsVo.getChildren();
            for(int i =0;i<opChildren_list.size();i++){
                QuestionOptionsVo opChildren = opChildren_list.get(i);
                opChildren.setPid(opId);
                this.add(opChildren,queID,i);
            }
        }
    }

    //改造update，有id的：中间表和children需要改；无id的：中间表和children需要插
    @Override
    public void update(QuestionOptionsVo questionOptionsVo,String queID,Integer sort) {
        SqlUtil<QuestionOptionsVo> sqlUtil = new SqlUtil<>();
        String sql = sqlUtil.createUpdateSelectiveSql(QuestionOptionsVo.class, questionOptionsVo);
        questionOptionsMapperVo.updateSelective(questionOptionsVo, sql);
        QuItOpVo quItOpVo = new QuItOpVo();
        quItOpVo.setItemId(queID);
        quItOpVo.setOpId(questionOptionsVo.getId());
        quItOpVo.setSort(sort);
        SqlUtil<QuItOpVo> sqlUtil_ItOp = new SqlUtil<>();
        sql = sqlUtil_ItOp.createUpdateSelectiveSql(QuItOpVo.class, quItOpVo);
        quItOpMapperVo.updateSelective(quItOpVo, sql);
        if(CollectionUtils.isNotEmpty(questionOptionsVo.getChildren())){
            List<QuestionOptionsVo> opChildren_list = questionOptionsVo.getChildren();
            for(int i =0;i<opChildren_list.size();i++){
                QuestionOptionsVo opChildren = opChildren_list.get(i);
                if(StringUtils.isEmpty(opChildren.getId())){
                    opChildren.setPid(questionOptionsVo.getId());
                    this.add(opChildren,queID,i);
                }else{
                    this.update(opChildren,queID,i);
                }
            }
        }
    }

    @Override
    public void update(QuestionOptionsVo questionOptionsVo) {

    }

    @Override
    public void updateAbsolute(QuestionOptionsVo questionOptionsVo) {
        SqlUtil<QuestionOptionsVo> sqlUtil = new SqlUtil<>();
        String sql = sqlUtil.createUpdateAbsoluteSql(QuestionOptionsVo.class);
        questionOptionsMapperVo.updateAbsolute(questionOptionsVo, sql);
    }

    @Override
    public void delById(String id) {
        questionOptionsMapperVo.delById(id);
    }
}
